package com.yuwei.module_patrol.view

import android.app.TimePickerDialog
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.MenuItem
import android.widget.PopupMenu
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.yuwei.module_patrol.R
import com.yuwei.module_patrol.adapter.EditTopAdapter
import com.yuwei.module_patrol.databinding.ActivityEditPatrolBinding

@Route(path = RouterPath.MODULE_EDIT_PATROL_ACTIVITY)
class EditPatrolActivity : BaseMvvmActivity<ActivityEditPatrolBinding>() {
    private val frequencyList : List<String> = listOf("每天","工作日","周末")
    private val statusList : List<String> = listOf("启用","停用")
    private val frequencyAdapter = EditTopAdapter()
    private val statusAdapter = EditTopAdapter()
    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView() {
        binding.editPatrolBack.setOnClickListener {
            finish()
        }
        val peopleMenu = PopupMenu(this, binding.editPatrolName)
        peopleMenu.menuInflater.inflate(R.menu.people_menu, peopleMenu.menu)
        peopleMenu.gravity = Gravity.BOTTOM
        binding.editPatrolName.setOnClickListener{
            peopleMenu.show()
        }
        peopleMenu.setOnMenuItemClickListener { item: MenuItem? ->
            binding.editPatrolPeople.text = item?.title
            false
        }
        val routeMenu = PopupMenu(this, binding.editPatrolRouteView)
        routeMenu.menuInflater.inflate(R.menu.route_menu, routeMenu.menu)
        routeMenu.gravity = Gravity.BOTTOM
        binding.editPatrolRouteView.setOnClickListener{
            routeMenu.show()
        }
        routeMenu.setOnMenuItemClickListener { item: MenuItem? ->
            binding.editPatrolRoute.text = item?.title
            false
        }
        binding.editPatrolStartTime.setOnClickListener {
            TimePickerDialog(this,
                { _, hourOfDay, minute ->
                    val time = "${hourOfDay}:${minute}"
                    binding.startTime.text = time
                },0,0,true).show()
        }
        binding.editPatrolEndTime.setOnClickListener {
            TimePickerDialog(this,
                { _, hourOfDay, minute ->
                    val time = "${hourOfDay}:${minute}"
                    binding.endTime.text = time
                },1,1,true).show()
        }
        frequencyAdapter.data.addAll(frequencyList)
        frequencyAdapter.notifyDataSetChanged()
        binding.patrolFrequencyRv.adapter = frequencyAdapter
        binding.patrolFrequencyRv.layoutManager = LinearLayoutManager(this,
            LinearLayoutManager.HORIZONTAL,false)
        frequencyAdapter.setOnItemClickListener { adapter, view, position ->
            frequencyAdapter.setPosition(position)
            frequencyAdapter.notifyDataSetChanged()
        }
        statusAdapter.data.addAll(statusList)
        statusAdapter.notifyDataSetChanged()
        binding.patrolStatusRv.adapter = statusAdapter
        binding.patrolStatusRv.layoutManager = LinearLayoutManager(this,
            LinearLayoutManager.HORIZONTAL,false)
        statusAdapter.setOnItemClickListener { adapter, view, position ->
            statusAdapter.setPosition(position)
            statusAdapter.notifyDataSetChanged()
        }
    }
    override fun observeDataChange() {
    }

    override fun getLayoutId(): Int = R.layout.activity_edit_patrol
}